package com.erp.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.order.domain.ErpSellerStoreNotautoProductSku;
import com.erp.order.domain.bo.ErpSellerStoreNotautoProductSkuBo;
import com.erp.order.domain.vo.ErpSellerStoreNotautoProductSkuVo;
import com.erp.order.mapper.ErpSellerStoreNotautoProductSkuMapper;
import com.erp.order.service.IErpSellerStoreNotautoProductSkuService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 店铺不自动化管理商品skuService业务层处理
 *
 * @author erp
 * @date 2024-02-26
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreNotautoProductSkuServiceImpl implements IErpSellerStoreNotautoProductSkuService {

    private final ErpSellerStoreNotautoProductSkuMapper baseMapper;

    /**
     * 查询店铺不自动化管理商品sku
     */
    @Override
    public ErpSellerStoreNotautoProductSkuVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询店铺不自动化管理商品sku列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreNotautoProductSkuVo> queryPageList(ErpSellerStoreNotautoProductSkuBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreNotautoProductSku> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreNotautoProductSkuVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询店铺不自动化管理商品sku列表
     */
    @Override
    public List<ErpSellerStoreNotautoProductSkuVo> queryList(ErpSellerStoreNotautoProductSkuBo bo) {
        LambdaQueryWrapper<ErpSellerStoreNotautoProductSku> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreNotautoProductSku> buildQueryWrapper(ErpSellerStoreNotautoProductSkuBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreNotautoProductSku> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getStoreId() != null, ErpSellerStoreNotautoProductSku::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreAlias()), ErpSellerStoreNotautoProductSku::getStoreAlias, bo.getStoreAlias());
        lqw.eq(StringUtils.isNotBlank(bo.getSkuCode()), ErpSellerStoreNotautoProductSku::getSkuCode, bo.getSkuCode());
        lqw.eq(StringUtils.isNotBlank(bo.getRevision()), ErpSellerStoreNotautoProductSku::getRevision, bo.getRevision());
        return lqw;
    }

    /**
     * 新增店铺不自动化管理商品sku
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreNotautoProductSkuBo bo) {
        ErpSellerStoreNotautoProductSku add = BeanUtil.toBean(bo, ErpSellerStoreNotautoProductSku.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改店铺不自动化管理商品sku
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreNotautoProductSkuBo bo) {
        ErpSellerStoreNotautoProductSku update = BeanUtil.toBean(bo, ErpSellerStoreNotautoProductSku.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreNotautoProductSku entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除店铺不自动化管理商品sku
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> insertBatchSkuCode(String skuCodes, Long storeId, String storeUserName) {
        //查询店铺是否存在
        ErpSellerStoreNotautoProductSku erpSellerStoreNotautoProductSku = new ErpSellerStoreNotautoProductSku();
        erpSellerStoreNotautoProductSku.setStoreId(storeId);
        erpSellerStoreNotautoProductSku.setStoreAlias(storeUserName);
        List<String> skuCodeList = Arrays.asList(skuCodes.trim().split(","));
        for (String skuCode : skuCodeList) {
            skuCode = skuCode.trim();
            
            ErpSellerStoreNotautoProductSku erpSellerStoreNotautoProductSkuSel = baseMapper.selectOne(
                    new LambdaQueryWrapper<ErpSellerStoreNotautoProductSku>()
                            .eq(ErpSellerStoreNotautoProductSku::getStoreId, storeId)
                            .eq(ErpSellerStoreNotautoProductSku::getSkuCode, skuCode)
            );
            if (ObjectUtils.isNotNull(erpSellerStoreNotautoProductSkuSel)){
                continue;
            }
            erpSellerStoreNotautoProductSku.setId(null);
            erpSellerStoreNotautoProductSku.setSkuCode(skuCode);
            baseMapper.insert(erpSellerStoreNotautoProductSku);
        }
        return R.ok("设置成功");
    }
}
